package com.jyh.blog.controller;

import com.jyh.blog.annotation.AccessLimit;
import com.jyh.blog.annotation.OptLog;
import com.jyh.blog.dto.OnlineUserDTO;
import com.jyh.blog.service.UserInfoService;
import com.jyh.blog.utils.Result;
import com.jyh.blog.vo.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.validation.Valid;

import static com.jyh.blog.constant.OptTypeConst.UPDATE;

@Api(tags = "用户信息管理")
@RestController
public class UserInfoController {
    @Autowired
    private UserInfoService userInfoService;

    /**
     * 修改用户角色
     * @param userRoleVO
     * @return
     */
    @OptLog(optType = UPDATE)
    @ApiOperation(value = "修改用户角色")
    @PutMapping("admin/user/role")
    public Result<String> editUserRole(@Valid @RequestBody UserRoleVO userRoleVO){
        userInfoService.editUserRole(userRoleVO);
        return Result.ok();
    }

    /**
     * 禁用、启用用户
     * @param userDisableVO
     * @return
     */
    @OptLog(optType = UPDATE)
    @ApiOperation(value = "禁用或启用用户")
    @PutMapping("admin/user/disable")
    public Result<String> disableUser(@Valid @RequestBody UserDisableVO userDisableVO){
        userInfoService.disableUser(userDisableVO);
        return Result.ok();
    }

    /**
     * 更新用户信息
     * @param userInfoVO
     * @return
     */
    @AccessLimit(seconds = 60,maxCount = 1)
    @ApiOperation(value = "更新用户信息")
    @PutMapping("user/info")
    public Result updateUserInfo(@Valid @RequestBody UserInfoVO userInfoVO){
        userInfoService.updateUserInfo(userInfoVO);
        return Result.ok();
    }

    /**
     * 绑定用户邮箱
     * @param emailVO
     * @return
     */
    @ApiOperation(value = "绑定用户邮箱")
    @PutMapping("user/email")
    public Result updateUserEmail(@Valid @RequestBody EmailVO emailVO){
        userInfoService.updateUserEmail(emailVO);
        return Result.ok();
    }

    /**
     * 更改用户头像
     * @param file
     * @return
     */
    @ApiOperation(value = "更改用户头像")
    @PostMapping("users/avatar")
    public Result<String> userAvatar(MultipartFile file){
        return Result.ok(userInfoService.changeAvatar(file));
    }

    /**
     * 在线用户
     * @param conditionVO
     * @return
     */
    @ApiOperation(value = "在线用户")
    @GetMapping("admin/users/online")
    public Result<PageResult<OnlineUserDTO>> onlineUser(ConditionVO conditionVO){
        return Result.ok(userInfoService.onlineUserList(conditionVO));
    }

    /**
     * 下线用户
     * @param userInfoId
     * @return
     */
    @ApiOperation(value = "下线用户")
    @DeleteMapping("admin/users/{userInfoId}/online")
    public Result removeOnlineUser(@PathVariable("userInfoId") String userInfoId){
        userInfoService.removeOnlineUser(userInfoId);
        return Result.ok();
    }

}
